{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "02b782b6-0d2b-4904-a287-7dd23d6ef66f",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Robodyno MagneticSensor API"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d01b0728-7e31-4bfc-b205-b7bd48abc0d5",
   "metadata": {},
   "source": [
    "## 1. 初始化磁导航传感器对象"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "dce58745-a428-4c2e-926f-4dc4eda53f1f",
   "metadata": {},
   "source": [
    "`MagneticSensor(can, id_)`\n",
    "\n",
    "参数：\n",
    "- `can` : can总线接口\n",
    "- `id_` : 磁导航传感器模块ID，默认`0x02`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "16a0fe4e-7655-435b-82b1-b556652acad6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from robodyno.components import MagneticSensor\n",
    "from robodyno.interfaces import CanBus\n",
    "can = CanBus()\n",
    "mag = MagneticSensor(can, 0x02)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "73d85177-0512-4cb3-ae26-396b2571be57",
   "metadata": {},
   "source": [
    "## 2. 磁场校准"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c736a6d1-f06e-4f97-8060-14a2566b2aa6",
   "metadata": {},
   "source": [
    "`calibrate_magnetic_field()`\n",
    "\n",
    "- 在新的使用环境下，磁导航传感器需`重新校准磁场`。\n",
    "  - 磁场校准是`记录新环境在没有磁条情况下的磁场大小`。\n",
    "  - 初次上电后应先`读取位置状态`，再进行`磁场校准`。\n",
    "  - 新的使用环境指: \n",
    "    - 温度发生很大变化（波动 20 度以上）\n",
    "    - 地理位置发生变化\n",
    "    - 传感器周边环境（磁性物质，电机转动等）等造成传感器周围磁场发生超过 10GS 磁场变化的环境。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "44fc23c3-23a2-4280-b2ac-ddbf425473e3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "mag.calibrate_magnetic_field()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8d2a2cb",
   "metadata": {},
   "source": [
    "## 3.读取位置状态"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "300089db",
   "metadata": {},
   "source": [
    "`get_position_status(timeout=None)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)\n",
    "\n",
    "返回值:\n",
    "\n",
    "- 磁导航传感器读取的位置状态，包含16位状态值的列表。\n",
    "  - 触发：1\n",
    "  - 未触发：0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fc13230d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mag.get_position_status()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
